/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.room;

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "ChatBean")
@ViewScoped

public class ChatBean implements Serializable{
    
    @ManagedProperty(value = "#{sessionUserBean}") // MY SESSION BEAN
    private SessionUserBean userSession;
    
    private final PushContext pushContext = PushContextFactory.getDefault().getPushContext();
    private String globalMessage;
    private String username;
    private String chatChannel;
    
    @PostConstruct
    void initBean(){
        this.username = userSession.getUserInfo().getUsername();
        
        if(userSession.isJoinedInTable())
        {
            chatChannel = "/" + userSession.getUserInfo().getCurrentTable().getTableKey() + "/chat";
        }
        else
        {
            chatChannel = "/chat";
        }
    }
    
    public void setUserSession(SessionUserBean userSession) {
        this.userSession = userSession;
    }
    
    public String getGlobalMessage() {
        return globalMessage;
    }

    public void setGlobalMessage(String globalMessage) {
        this.globalMessage = globalMessage;
    }

    public String getUsername() {
        return username;
    }

    public void sendGlobal() {
        pushContext.push(chatChannel, username + ": " + globalMessage);
        globalMessage = null;
    }
    
}
